from rest_framework.views import APIView
from fdfs_client.client import Fdfs_client
from django.conf import settings
from rest_framework.response import Response
from rest_framework import status
from django.shortcuts import render,redirect

class UploadImageView(APIView):
    def post(self,request):
        client = Fdfs_client(settings.FDFS_CLIENT_CONF)

        res = client.upload_by_buffer(request.data.get('img').read())
        if res.get('Status') != 'Upload successed.':
            raise Exception('上传文件失败')
        file_id = res.get('Remote file_id')
        print(settings.FDFS_URL + file_id)
        return Response({'imgurl': settings.FDFS_URL + file_id})


from django.http import HttpResponseRedirect
class CommonImageView(APIView):
    def post(self,request):


        client_conf=settings.FDFS_CLIENT_CONF
        client=Fdfs_client(client_conf)

        data = request.data.get('upload')
        res=client.upload_by_buffer(data.read())
        if res.get('Status') != 'Upload successed.':
            raise Exception('上传文件失败')
        file_id = res.get('Remote file_id')
        base_url=settings.FDFS_URL
        file_name= base_url+file_id

        CKEditorFuncNum = request.query_params.get('CKEditorFuncNum')
        ret_url='http://www.tensquare.site:8080/upload_success.html?image_url=' + file_name + '&CKEditorFuncNum=' + CKEditorFuncNum



        return HttpResponseRedirect(ret_url)